import express from 'express';
import dotenv from 'dotenv';
import path from 'path';
import cors from 'cors';

import connectDB from './config/db';
import {
    errorResponserHandler,
    invalidPathHandler
} from './middleware/errorHandler';
//routes
import userRoutes from './routes/userRoutes';
import postRoutes from './routes/postRoutes';
import commentRoutes from './routes/commentRoutes';
import postCategoriesRoutes from './routes/postCategoriesRoutes';

dotenv.config();
connectDB();
const app = express();
app.use(express.json());
app.use(cors());

app.get('/', (req, res) => {
    res.send("running");
});

app.use('/users', userRoutes);
app.use('/posts', postRoutes);
app.use('/comments', commentRoutes);
app.use('/post-categories', postCategoriesRoutes);

app.use('/uploads', express.static(path.join(__dirname, '/uploads')))

app.use(invalidPathHandler);
app.use(errorResponserHandler);

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => console.log(`server running on port ${PORT}`));